export default ({ $axios, store }, inject) => {
  //请求拦截
  $axios.interceptors.request.use(
    (config) => {
      config.headers = {
        token: store.getters.token
      }
      if (
        config.method === 'post' ||
        config.method === 'put' ||
        config.method === 'delete'
      ) {
        config.data = {
          ...config.data
        }
      } else if (config.method === 'get') {
        config.params = {
          ...config.params
        }
      }
      return config
    },
    (error) => {
      // 对请求错误做些什么
      return Promise.reject(error)
    }
  )
  //相应拦截
  $axios.interceptors.response.use(
    (response) => {
      //返回结果
      return response.data
    },
    (err) => {
      const status = err.response.status
      //其他错误情况统一处理
      const errorMsg = {
        code: -1,
        msg: '服务器内部错误',
        data: {
          status: status,
          message: err.response.statusText
        }
      }
      //返回数据
      return errorMsg
    }
  )
  //请求Api接口
  inject('http', (url, data = {}, method = 'GET') => {
    let baseURL = ''
    if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'uat') {
      baseURL = 'https://bisapi.dev.shijiayun.cn/'
    } else {
      baseURL = 'https://bisapi.shijiayun.cn/'
    }
    if (method == 'GET') {
      return $axios.get(`${baseURL}${url}`, {
        params: data
      })
    } else {
      return $axios.post(`${baseURL}${url}`, data)
    }
  })
}